package rmhc.gui
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	import rmhc.RMHCApp;
	import rmhc.api.dataflow.PotatoeInfo;
	
	public class StatusCell extends MovieClip
	{
		public const whiteAlpha:Number = 0.4;
		
		public var red:Shape;
		public var white:Shape;
		public var thumb:Loader;
		
		protected var act:Boolean = false;
		protected var info:PotatoeInfo;
		
		public function StatusCell( info:PotatoeInfo )
		{
			super();
			this.info = info;
			
			mouseChildren = false;
			buttonMode = true;
			
			red = new Shape();
			var g:Graphics = red.graphics;
			g.beginFill( 0xBD1A1D , 1 );
			g.drawRect( 0 , 0 , 80 , 50 );
			g.endFill();
			addChild( red );
			
			thumb = new Loader();
			thumb.alpha = 0;
			addChild( thumb );
			
			white = new Shape();
			g = white.graphics;
			g.beginFill( 0xFFFFFF , 1 );
			g.drawRect( 0 , 0 , 80 , 50 );
			g.endFill();
			addChild( white );
			white.alpha = whiteAlpha;
			
			addEventListener( MouseEvent.MOUSE_OVER , onOver );
			addEventListener( MouseEvent.MOUSE_OUT , onOut );
		}
		
		public function init():void
		{
			//грузим картинку
			var req:URLRequest = new URLRequest( RMHCApp.getServerUrl() + info.thumb );
			thumb.contentLoaderInfo.addEventListener(Event.COMPLETE , onLoadThumb );
			thumb.load( req );
		}
		
		public function getInfo():PotatoeInfo
		{
			return info;
		}
		public function getThumb():BitmapData
		{
			return ( thumb.content as Bitmap ).bitmapData.clone();
		}
		public function getThumbSrc():String
		{
			return thumb.contentLoaderInfo.url;
		}
		
		public function setActive( act:Boolean ):void
		{
			this.act = act;
			var a:Number = act ? 0 : whiteAlpha;
			tween( a );
		}
		
		protected function onLoadThumb( e:Event ):void
		{
			TweenLite.to( thumb , 0.8 , { alpha : 1 } );
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		protected function onOver( e:MouseEvent ):void
		{
			if ( act )
			{
				return;
			}
			tween( 0 );
		}
		protected function onOut( e:MouseEvent ):void
		{
			if ( act )
			{
				return;
			}
			tween( whiteAlpha );
		}
		
		protected function tween( a:Number ):void
		{
			TweenLite.to( white , 0.5 , { alpha : a } );
		}
	}
}